REM Modify a Notes file
REM Title:      Director:Menus.IntNotes.Modify
REM Author:     Philip Ludlam
REM Version:    0.31 Beta, Saturday the 3rd of November, 2001
REM Copyright:  (C) Philip Ludlam 2001

REM This program is free software; you can redistribute it and/or modify it
REM under the terms of the GNU General Public License as published by the Free
REM Software Foundation; either version 2 of the License, or (at your option)
REM any later version.
REM
REM This program is distributed in the hope that it will be useful, but WITHOUT
REM ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
REM FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
REM details.
REM
REM You should have received a copy of the GNU General Public License along with
REM this program; if not, write to the Free Software Foundation, Inc., 59 Temple
REM Place - Suite 330, Boston, MA 02111-1307, USA

ON ERROR PROCerror
REM *SPOOL scsi::4.$.DATA
REM TRACE ON

DIM block% 1023,syntax% 255,output% 255,file_area% 2047
file_size%=0:file_ptr%=0:file%=0

Menu=FNswi_number("Director_Menu")
EndMenu=FNswi_number("Director_EndMenu")
Option=FNswi_number("Director_Option")
Command=FNswi_number("Director_Command")
Dash=FNswi_number("Director_Dash")

REM Get command line
SYS "OS_GetEnv" TO comm%
REM VDU4:PRINT FNstring(comm%)
SYS "OS_ReadArgs","name,quit,file/k,line/k,add/k,edit/k,remove/s,more/k,move/k,priority/k,sub/k,notes/k",comm%,block%,1023
IF block%!8=0 THEN
  file$="Director:Menus.IntNotes.Notes.Notes"
ELSE
  file$=FNstring(block%!8)
  IF INSTR(file$,"$")=0 AND INSTR(file$,":")=0 file$="Director:Menus.IntNotes.Notes."+file$
ENDIF
IF block%!12<>0 line%=EVAL(FNstring(block%!12)) ELSE line%=0
IF block%!16<>0 add$=FNstring(block%!16) ELSE add$=""
IF block%!20<>0 edit$=FNstring(block%!20) ELSE edit$=""
IF block%!24<>0 remove$="r" ELSE remove$=""
REM IF block%!28<>0 more$=FNlcase(FNstring(block%!28)) ELSE more$=""
IF block%!32<>0 move$=FNlcase(FNstring(block%!32)) ELSE move$=""
IF block%!36<>0 priority$=FNlcase(FNstring(block%!36)) ELSE priority$=""
IF block%!40<>0 sub$=" "+FNstring(block%!40) ELSE sub$=""
IF block%!44=0 THEN
  notes$=""
ELSE
  notes$=FNstring(block%!44)
  IF INSTR(notes$,"$")=0 AND INSTR(notes$,":")=0 THEN
    notes$="Director:Menus.IntNotes.Notes."+notes$
  ENDIF
ENDIF


IF FNfile_valid(file$,1) THEN
  PROCfile_load(file$)
  file_lines%=FNfile_countlines
  DIM file_line$(file_lines%)
  IF file_lines%>0 THEN
    file_ptr%=0
    FOR a%=1 TO file_lines%
      file_line$(a%)=FNfile_read
    NEXT
  ENDIF
ELSE
  file_lines%=0
  DIM file_line$(file_lines%)
ENDIF

REM Add
IF add$<>"" THEN
  file_line$(0)=""""+add$+""""
ENDIF

REM Remove
IF remove$<>"" THEN
  file_line$(line%)=""
ENDIF

REM Move
IF move$<>"" THEN
  REM Move line
  CASE move$ OF
    WHEN "top" :
      FOR a%=line% TO 1 STEP -1
        PROCswap(a%,a%-1)
      NEXT
    WHEN "up" : PROCswap(line%,line%-1)
    WHEN "down" : PROCswap(line%,line%+1)
    WHEN "bottom" :
      FOR a%=line% TO file_lines%
        PROCswap(a%,a%+1)
      NEXT
  ENDCASE
ENDIF

IF edit$<>"" OR priority$<>"" OR sub$<>"" OR notes$<>"" THEN
  REM Get current/old values
  SYS "OS_ReadArgs","/a,priority/k,sub/k,notes/k",file_line$(line%),block%,1024
  old_edit$=FNstring(!block%)
  IF block%!4<>0 old_priority$=" -priority "+FNstring(block%!4) ELSE old_priority$=""
  IF block%!8<>0 old_sub$=" -sub "+FNstring(block%!8) ELSE old_sub$=""

  IF block%!12=0 THEN
    old_notes$=""
  ELSE
    old_notes$=FNstring(block%!12)
    IF INSTR(old_notes$,"$")=0 AND INSTR(old_notes$,":")=0 THEN
      old_notes$="Director:Menus.IntNotes.Notes."+old_notes$
    ENDIF
  ENDIF

  REM Edit
  IF edit$<>"" old_edit$=edit$

  REM Priority
  IF priority$<>"" THEN
    IF priority$="standard" THEN
      old_priority$=""
    ELSE
      IF priority$<>"" THEN old_priority$=" -priority "+priority$
    ENDIF
  ENDIF

  REM Sub/More
  IF sub$<>"" old_sub$=" -sub"+sub$
  IF sub$=" " old_sub$=""

  REM Notes
  IF notes$<>"" AND old_notes$<>"" THEN
    IF FNfile_valid(notes$,0) AND FNfile_valid(old_notes$,1) THEN
      OSCLI("Copy "+FNfile_canon(old_notes$)+" "+FNfile_canon(notes$)+" ~CDF~V")
    ENDIF
  ENDIF
  IF notes$="Director:Menus.IntNotes.Notes." old_notes$=""
  IF notes$<>"Director:Menus.IntNotes.Notes." AND notes$<>"" THEN
    old_notes$=notes$
  ENDIF
  IF LEFT$(old_notes$,30)="Director:Menus.IntNotes.Notes." THEN
    old_notes$=MID$(old_notes$,31)
  ENDIF
  IF old_notes$<>"" old_notes$=" -notes """+old_notes$+""""

  file_line$(line%)=""""+old_edit$+""""+old_priority$+old_sub$+old_notes$
ENDIF

REM Save notes
IF FNlcase(LEFT$(file$,9))="director:" file$="<Director$Write>."+MID$(file$,10)
IF FNlcase(LEFT$(file$,15))="<director$path>" file$="<Director$Write>"+MID$(file$,15)
IF remove$<>"" AND file_lines%=1 THEN
  OSCLI("X Delete "+file$)
ELSE
  SYS "XOS_Find",&8C,file$ TO file%;f%
  IF (f% AND 1)=1 THEN
    SYS Menu,"Notes Notes_Temp -temp"
    SYS Option,"""Cannot open menu file"" -grey"
    SYS EndMenu
    *DirectorShowMenu Notes_Temp
  ELSE
    IF file_lines%<>0 THEN
      FOR a%=1 TO file_lines%
        IF  file_line$(a%)<>"" BPUT #file%, file_line$(a%)
      NEXT
    ENDIF
    IF add$<>"" BPUT #file%, file_line$(0)
    CLOSE#file%:file%=0
    OSCLI("X SetType "+file$+" Text")
  ENDIF
ENDIF

END

:

DEF PROCswap(a%,b%)
IF a%<1 OR a%>file_lines% ENDPROC
IF b%<1 OR b%>file_lines% ENDPROC
SWAP file_line$(a%),file_line$(b%)
ENDPROC

:

DEF FNswi_number(n$)
SYS "OS_SWINumberFromString",,n$ TO a%
=a%

:

DEF FNstring(p%):LOCAL a$:a$=""
WHILE ?p%>31
  a$+=CHR$(?p%):p%+=1
  ENDWHILE:=a$

:

DEF FNstring_c(s%,l%):LOCAL a$,p%:a$="":p%=0
WHILE s%?p%>31 AND p%<=l%
  a$+=CHR$(s%?p%):p%+=1
  ENDWHILE:=a$

:

DEF PROCfile_load(file$)
LOCAL len%
SYS "OS_File",16,file$,file_area% TO ,,,,len%
file_area%?len%=0
file_size%=len%
ENDPROC

:

DEF FNfile_read
LOCAL i$:i$=""
WHILE i$="" AND file_ptr%<=file_size%
  i$=FNstring_c(file_area%+file_ptr%,file_size%-file_ptr%)
  file_ptr%+=LEN(i$)+1
  ENDWHILE
=i$

:

DEF FNfile_countlines
n%=0
WHILE FNfile_read<>""
  n%+=1
  ENDWHILE
=n%

:

DEF FNfile_canon(file$)
IF FNlcase(LEFT$(file$,9))="director:" THEN
  IF FNfile_valid("<Director$Dir>."+MID$(file$,10),1) THEN
    file$="<Director$Dir>."+MID$(file$,10)
  ELSE
    file$="<Director$Write>."+MID$(file$,10)
  ENDIF
ENDIF
=file$

:

DEF FNlcase(text$)

LOCAL Loop%,text2$
text2$=STRING$(LEN(text$)," ")
text2$=""

FOR Loop%=1 TO LEN(text$)
  chr%=ASC(MID$(text$,Loop%,1))
  IF chr%>64 AND chr%<91 chr%=chr%+32
  text2$+=CHR$(chr%)
NEXT

=text2$

:

DEF PROCerror
ON ERROR OFF
VDU4
IF file%<>0 CLOSE#file%
ON ERROR OFF
VDU 4
PRINT "Error in: !Director.Menus.IntNotes.Modify"
PRINT "Report:   ";REPORT$
PRINT "Line:     ";ERL
END

:

REM want% = to what object type you want to find/are looking for
DEF FNfile_valid(file$,want%):LOCAL ans%
SYS "XOS_File",17,file$ TO ans%;flags%
IF (flags% AND 1) THEN =FALSE ELSE =(ans%=want%)

:
